$(document).ready(function() {
	//~判断菜式是否超时=====================================================================
	var isTimeOut = $('#is-time-out').html();
	//console.log(isTimeOut);
	if (isTimeOut == 'true') {
		alert('部分菜式在这个时间段已经不能订购,请再次填写你的订单');
		location.href='/starfood/user/order';
	}
	
	//~为每个已经删除的菜式后面添加顿号,最后一个不添加=====================================================================
	$('.dunhao:not(:last)').html('、');

	//~为菜式统计总价=====================================================================
	//~初始化菜式数量,菜式数量input项已经由velocity初始化,不需要再初始化=====================================================================
	$('.dish-count').each(function() {
		var dishCount = $(this).html();
		var dishPrice = $(this).siblings('.dish-price-span').html();
		var dishOneTotalSpan = $(this).siblings('.total');

		var total = dishPrice * dishCount;
		total = roundNumber(total, 2);
		dishOneTotalSpan.html(total);
		//统计所有菜式总价
		setAllTotal();
	});

	//~菜式数量加1=====================================================================
	$('.plus-count').click(function() {
		var dishCountSpan = $(this).siblings('.dish-count');
		var dishCount = dishCountSpan.html();
		
		var dishCountInput = $(this).siblings('.count-input');
		
		var dishPrice = $(this).siblings('.dish-price-span').html();
		var dishOneTotalSpan = $(this).siblings('.total');
		
		dishCount++;
		var total = dishPrice * dishCount;
		total = roundNumber(total, 2);
		
		dishOneTotalSpan.html(total);
		
		dishCountSpan.html(dishCount);
		dishCountInput.val(dishCount);
	});
	
	//~菜式数量减1=====================================================================
	$('.minus-count').click(function() {
		var dishCountSpan = $(this).siblings('.dish-count');
		var dishCount = dishCountSpan.html();
		
		var dishPrice = $(this).siblings('.dish-price-span').html();
		var dishOneTotalSpan = $(this).siblings('.total');
		
		if (dishCount > 0) {
			dishCount--;
			
			var total = dishPrice * dishCount;
			total = roundNumber(total, 2);
			
			dishOneTotalSpan.html(total);
			
			dishCountSpan.html(dishCount);
		}
	});
	
	//~统计所有菜式总价=====================================================================
	function setAllTotal() {
		var allTotal = 0;
		$('.total').each(function() {
			allTotal = allTotal + Number($(this).html());
		});
		allTotal = roundNumber(allTotal, 2);
		$('#all-dish-total').html(allTotal);
	};
	
	$('.minus-count,.plus-count').click(function() {
		setAllTotal();
	});
});